﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace D1
{
    delegate int Fun(Order x, Order y);
    class Good
    {
        public string name { set; get; }
        public double price { set; get; }
        public Good(int kind,double price)
        {
            this.price = price;
            switch (kind)
            {
                case 0: name = "good1"; break;
                case 1: name = "good2";  break;
                case 2: name = "good3";  break;
                case 3: name = "good3"; break;
                case 4: name = "good3"; break;
            }
        }
        public override string ToString()
        {
            return ("name：" + this.name + "price：" + this.price);
        }
    }
    class Client
    {
        public string name { set; get; }
        public int age { set; get; }
        public string phoneNumber { set; get; }
        public Client(string name, int age, string phoneNumber)
        {
            this.name = name;
            this.age = age;
            this.phoneNumber = phoneNumber;
        }
        public override string ToString()
        {
            return ("name：" + this.name + "age：" + this.age+"电话号码:"+ this.phoneNumber);
        }
    }
    class OrderDetail
    {
        public Good good;
        public double amount;
        public double money;
        public OrderDetail(double amount, double price, int kind)
        {
            this.good = new Good(kind,price);
            this.amount = amount;
            this.money = amount* price;
        }
        public override string ToString()
        {
            return ("购买商品:"+good.ToString()+"数量:"+ amount+"价值："+ money);
        }
        public override bool Equals(Object obj)
        {
            OrderDetail Detail = obj as OrderDetail;
            bool ss = (this.good.name == Detail.good.name);
            return ss;
        }//一种物品只有一个清单
        public override int GetHashCode()
        {
            return Convert.ToInt32(good.name);
        }
    }
    class Order
    {
        public string id { set; get; }
        public string date { set; get; }
        public double totalMoney { set; get; }
        public Client client;
        public List<OrderDetail> details;
        public override string ToString()
        {
            return ("订单号："+id+"时间："+date+"总金额"+totalMoney);
        }
        public string getGoods()
        {
            string str = string.Empty;
            foreach(var item in details)
            {
                str += item.good.name;
            }
            return str;
        }
        public void showOrder()
        {
            Console.WriteLine("订单号  客户  日期  总金额");
            Console.WriteLine("----------------------------");
            Console.WriteLine("{0} {1} {2} {3}", id, client.name, date, totalMoney);
            Console.WriteLine("客户详细信息：");
            Console.WriteLine(client.ToString());
            foreach (var item in details)
            {
                Console.WriteLine("订单明细：");
                Console.WriteLine(item.ToString());
            }
            Console.WriteLine("#####################################");
        }
        public Order (string id, string date)
        {
            this.id = id;
            this.date = date;
            details = new List<OrderDetail>();
        }
        public Order()
        {
        }
        public void changeClient(string name, int age, string phoneNumber)
        {
            client.age = age;
            client.name = name;
            client.phoneNumber = phoneNumber;
        }
        public double culculateMoney()
        {
            totalMoney = 0;
            foreach(var item in details)
            {
                totalMoney += item.money;
            }
            return totalMoney;
        }
        public override bool Equals(Object obj)
        {
            Order order = obj as Order;
            return order.id == this.id;
        }
        public override int GetHashCode()
        {
            return Convert.ToInt32(id);
        }
        public void setClient(string name, int age, string phoneNumber)
        {
            client = new Client( name,  age,  phoneNumber);
        }
        public void addDetails(double amount, double money, int kind)
        {
            details.Add(new OrderDetail( amount,  money,  kind));
        }
        public void addDetails(OrderDetail detail)
        {
            details.Add(detail);
        }
    }
    class OrderService
    {
       private List<Order> orders;
        public OrderService()
        {
            orders = new List<Order>();
        }
        public void showAll()
        {
            foreach(var item in orders)
            {
                item.showOrder();
            }
        }
        public void addOrder()
        {
            Console.WriteLine("请输入订单编号：");
            string id = Console.ReadLine();
            Console.WriteLine("请输入时间：");
            string date = Console.ReadLine();
            Order a = new Order(id, date);
            foreach (var item in orders)
            {
                if (item.Equals(a))
                {
                    Console.WriteLine("此订单已存在");return;
                }
            }

            Console.WriteLine("输入订单明细：");
            Console.WriteLine("请输入客户姓名：");
            string name = Console.ReadLine();
            Console.WriteLine("请输入客户手机号码：");
            string phoneNumber = Console.ReadLine();
            Console.WriteLine("请输入客户年龄：");
            int age = int.Parse(Console.ReadLine());
            a.setClient( name,  age,  phoneNumber);

            char conti = 'y'; double amount, money; int kind;
            while (conti == 'y')
            {
                Console.WriteLine("请输入种类：");
                 kind = int.Parse(Console.ReadLine());
                Console.WriteLine("请输入购买数量：");
                 amount = double.Parse(Console.ReadLine());
                Console.WriteLine("请输入交易价格：");
                 money = double.Parse(Console.ReadLine());
                OrderDetail detail = new OrderDetail(amount, money, kind);
                bool unrepeat = true;
                foreach (var item in a.details)
                {
                    if (item.Equals(detail))
                    {
                        unrepeat = false;break;
                    }
                }
                if (unrepeat)
                {
                    a.addDetails(amount, money, kind);
                    Console.WriteLine("加入另一个明细表？ y/n ");
                    conti = char.Parse(Console.ReadLine());
                }
                else
                {
                    Console.WriteLine("明细表重复，请重新输入 ");
                }
                
            }
            a.culculateMoney();
            orders.Add(a);
        }
        public void removeOrder()
        {
            try
            {
                Console.WriteLine("请输入订单号");
                string id = Console.ReadLine();
                var selected = from item in orders where item.id == id select item;
                // Order it = (Order)selected;
                Order it=new Order();
                foreach(var item in selected)
                {
                    it = item;
                }
                orders.Remove(it);
            }
            catch (System.Exception ex){
                Console.WriteLine("输入错误:");
                Console.WriteLine(ex.Message);
            }
        }
        
        public void  searchOrder()
        {
            Console.WriteLine("请输入1,2,3，或4，分别根据订单号、商品名称、客户、订单金额进行查询");
            int chose = int.Parse(Console.ReadLine()); bool yes = false;
            switch (chose)
            {
                case 1:
                    Console.WriteLine("请输入订单号");
                    string id = Console.ReadLine();
                    var selected = from item in orders where item.id == id
                                   orderby item.totalMoney select item;
                   
                    foreach (var it in selected)
                    {
                        yes = true;
                        it.showOrder();
                        Console.WriteLine("---------------------");
                    }break;
                    
                case 2:
                    Console.WriteLine("请输入商品名称");
                    string goodname = Console.ReadLine();
                    var selected2 = from item in orders where item.getGoods().Contains(goodname)
                                    orderby item.totalMoney select item;
                    foreach (var it in selected2)
                    {
                        it.showOrder();
                        Console.WriteLine("---------------------");
                    }
                    break;
                case 3:
                    Console.WriteLine("请输入客户名");
                    string clientname = Console.ReadLine();
                    var selected3 = from item in orders where item.client.name== clientname
                                    orderby item.totalMoney select item;
                    foreach (var it in selected3)
                    {
                        it.showOrder();
                        Console.WriteLine("---------------------");
                    }
                    break;
                case 4:
                    Console.WriteLine("请输入订单金额");
                    double money = double.Parse(Console.ReadLine());
                    var selected4 = from item in orders
                                    where item.totalMoney == money
                                    orderby item.totalMoney
                                    select item;
                    foreach (var it in selected4)
                    {
                        it.showOrder();
                        Console.WriteLine("---------------------");
                    }
                    break;
            }
            if (yes == false)
            {
                Console.WriteLine("无此订单");
            }
        }
        public void changeOrder()
        {
            try
            {
                Console.WriteLine("请输入订单号");
                string id = Console.ReadLine();
                Console.WriteLine("请输入1或2,修改订单客户或明细");
                int chose = int.Parse(Console.ReadLine());
                var selected = from item in orders where item.id == id select item;
                foreach (var it in selected)
                {
                    switch (chose)
                    {
                        case 1:
                            Console.WriteLine("请输入姓名");
                            string name = Console.ReadLine();
                            Console.WriteLine("请输入年龄");
                            int age = int.Parse(Console.ReadLine());
                            Console.WriteLine("请输入联系方式");
                            string phoneNumber = Console.ReadLine();
                            it.changeClient( name,  age,  phoneNumber);
                            break;
                        case 2:
                            it.details.Clear();
                            char conti = 'y'; double amount, money; int kind;
                            while (conti == 'y')
                            {
                                Console.WriteLine("请输入种类：");
                                kind = int.Parse(Console.ReadLine());
                                Console.WriteLine("请输入购买数量：");
                                amount = double.Parse(Console.ReadLine());
                                Console.WriteLine("请输入交易金额：");
                                money = double.Parse(Console.ReadLine());
                                OrderDetail detail = new OrderDetail(amount, money, kind);
                                bool unrepeat = true;
                                foreach (var item in it.details)
                                {
                                    if (item.Equals(detail))
                                    {
                                        unrepeat = false; break;
                                    }
                                }
                                if (unrepeat)
                                {
                                    it.addDetails(amount, money, kind);
                                    Console.WriteLine("加入另一个明细表？ y/n ");
                                    conti = char.Parse(Console.ReadLine());
                                }
                                else
                                {
                                    Console.WriteLine("明细表重复，请重新输入 ");
                                }

                            }break;
                    }
                    it.culculateMoney();
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("输入错误:");
                Console.WriteLine(ex.Message);
            }
        }
       // delegate int FUN(Order x, Order y);
        public void sortOrder(int i=0)
        {
            switch (i)
            {
                case 0:
                    orders.Sort((x, y) => (int.Parse(x.id)-int.Parse(y.id)));break;
                case 1:
                    orders.Sort((x, y) => (int.Parse(x.date) - int.Parse(y.date))); break;
                case 2:
                    orders.Sort((x, y) => Convert.ToInt32(x.totalMoney - y.totalMoney)); break;
            }
            //Comparison<Order> comparison
            //orders.Sort(comparison);
            //orders.Sort((x,y)=> Convert.ToInt32(x.totalMoney-y.totalMoney));
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            OrderService service = new OrderService();
            bool not_quit = true;
            while (not_quit)
            {
                Console.WriteLine("输入1增加订单，输入2删除订单，输入3查询订单\n输入4显示所有订单，输入5根据订单号为订单排序,输入6根据日期为订单排序,输入7根据总金额为订单排序\n输入8改变订单，输入9退出");
                string choose1 = Console.ReadLine();
                switch (choose1)
                {
                    case "1": service.addOrder(); break;
                    case "2": service.removeOrder(); break;
                    case "3": service.searchOrder(); break;
                    case "4": service.showAll(); break;
                    case "5": service.sortOrder(0); break;
                    case "6": service.sortOrder(1); break;
                    case "7": service.sortOrder(2); break;
                    case "8": service.changeOrder();break;
                    case "9": not_quit = false; break;
                    default: Console.WriteLine("输入错误"); break;
                }
            }


        }
    }
}
